-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Added support for platform.system() #8461
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
emmatyping
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here are my initial comments, I think you have the right idea, but there are some things that need to be fixed.
| # The executable used to search for PEP 561 packages. If this is None, | ||
| # then mypy does not search for PEP 561 packages. | ||
| self.python_executable = sys.executable # type: Optional[str] | ||
| self.platform = sys.platform |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems you didn't rename this to be consistent with your usage below. You should also change any usage of this elsewhere.
| self.platform = sys.platform | |
| self.sys_platform = sys.platform |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like you missed updating this based on the test failure. In your call to consider_sys_platform you use options.sys_platform, but here you are assigning to options.platform those two need to be consistently named.
mypy/reachability.py
Outdated
| result = consider_sys_version_info(expr, pyversion) | ||
| if result == TRUTH_VALUE_UNKNOWN: | ||
| result = consider_sys_platform(expr, options.platform) | ||
| result = consider_sys_platform(expr, options.sys_platform) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You seem not to be passing options.platform_system here, but you added it as an argument below.
mypy/reachability.py
Outdated
| def consider_sys_platform(expr: Expression, platform: str) -> int: | ||
| """Consider whether expr is a comparison involving sys.platform. | ||
| def consider_sys_platform(expr: Expression, platform: str, platform_system: str) -> int: | ||
| """Consider whether expr is a comparison involving sys.platform |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please update the docstring to add that this also considers platform.system (see below).
mypy/reachability.py
Outdated
| # sys.platform is of str class and platform.system is of | ||
| # function class | ||
| if isinstance(expr, MemberExpr) and expr.name == name: | ||
| if isinstance(expr.expr, CallExpr) and expr.expr.name == 'system': |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be if isinstance(expr.expr, CallExpr) and expr.expr.name == 'platform'?
| elif platform: | ||
| if not is_sys_attr(expr.operands[0], 'platform'): | ||
| return TRUTH_VALUE_UNKNOWN | ||
| right = expr.operands[1] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems over-indented, it gets used below.
mypy/reachability.py
Outdated
| @@ -1,5 +1,5 @@ | |||
| """Utilities related to determining the reachability of code (in semantic analysis).""" | |||
|
|
|||
| import platform | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this import is needed?
| import sys | ||
| if sys.platform == 'fictional': | ||
| def foo() -> int: return 0 | ||
| if platform.system(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| if platform.system(): | |
| if platform.system() == 'fake': |
I think this test should test comparison.
|
@ethanhs thanks for reviewing this commit, will work on it ASAP |
|
@ethanhs I've added the requested changes, please let me know what else needs to be done, and then I'll start to fix the checks! |
|
@ethanhs I've renamed the variable, but I still think there is some logic inconsistencies |
|
@joybhallaa I will take a look at this later. |
| return TRUTH_VALUE_UNKNOWN | ||
| right = expr.operands[1] | ||
| # check if either platform or platform_system is being used | ||
| if platform_system: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right now you are saying the truth value is unknown if the expression is not a platform attribute, but you don't want to do that. Both platform and platform_system should be truthy, since they will both be set in options.
|
Also, user configuration/command line input should override this. I'm not certain what exactly to do here, but I think mapping Looking at the There is some awkwardness with cygwin https://stackoverflow.com/a/32612402 and I would be surprised if anyone uses mypy on AIX, so I wouldn't worry about those. |
JukkaL
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think mapping sys.platform values to platform.system() values would be best.
I agree that this seems like the best option. Something like if platform.system() == 'Linux' should always mean exactly the same as if sys.platform == 'linux'. Thus we don't need to (and shouldn't) import platform at runtime.
| if sys.platform == 'fictional': | ||
| def foo() -> int: return 0 | ||
| if platform.system() == 'fake': | ||
| def foo() -> int: return 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should also be a test that has a successful platform check.
Also test that --platform affects a platform.system() check.
|
PR is pretty stale, feel free to re-open if you want to pick this back up and address feedback / broken tests |
What is this PR about ?
This PR is related to #8166 .
@brettcannon asked to include support for
platform.system()with the existingsys.platformThings I've Changed
platformtosys_platformto avoid confusionplatform = platform.system()in options.pyplatforminconsider_sys_platformfunctionTests
I've checked the changes with
pep8(pycodestyle)I think changing the
platformtosys_platformmay cause some errors if it is being used somewhere else.